package org.vectrics.recruiter.struts.tag;

import org.apache.log4j.Logger;
import org.vectrics.common.html.Table;
import org.vectrics.company.CompanyPerson;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.user.Person;

/**
 * @author mmoore
 */
public class UserBufferForJobTag extends UserBufferTag {

	private static final long serialVersionUID = -5173657084527130789L;
	
	private transient final Logger log = Logger.getLogger(this.getClass());
    private String jobId = null;
    
    private Job getJob() {
        Job job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
        return(job);
    }
    
    protected void createJobBufferContent(Table contentTable, Job jobForBuffer) {
        
    }
    
    protected void createPersonBufferContent(Table contentTable, Person personForBuffer) {
        if (jobId != null) {
        	CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(personForBuffer);
        	if (candidate != null) {
	        	if (getJob().findJobCandidate(candidate) == null) {
			    	String href = this.getContextPath() + "/admin/jobs/candidate/add.do?jobId=" + getJob().getId() 
			    	        + "&userId=" + personForBuffer.getId();
			    	this.makeLinkRow(contentTable, this.getPhrase("phrase.make.candidate.for.job"), href);
	        	} else {
	        		log.debug("Person is already candidate for job");
	        	}
        	} else {
        		CompanyPerson employee = RecruitServiceLocator.getCompanyService().findEmployeeForIdPerson(personForBuffer.getId());
        		if (employee==null) {
        			String href = this.getContextPath() + "/admin/jobs/candidate/add.do?jobId=" + getJob().getId() 
        			+ "&userId=" + personForBuffer.getId();
        			this.makeLinkRow(contentTable, this.getPhrase("phrase.make.candidate.for.job"), href);        			
        		}
        	}
        	
        } else {
            log.debug("Job ID not specified, can't make candidate link");
        }
        
        
    }
    
    /**
     * @return Returns the jobId.
     */
    public String getJobId() {
        return jobId;
    }
    /**
     * @param jobId The jobId to set.
     */
    public void setJobId(String jobId) {
        this.jobId = jobId;
    }
}
